package com.dfbz.dao;

import com.dfbz.dao.base.BaseDao;
import com.dfbz.model.Dept;
import com.dfbz.utils.JDBCUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class DeptDao extends BaseDao {

    public List<Dept> findAll() {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        RowProcessor processor = new BasicRowProcessor(new GenerousBeanProcessor());
        List<Dept> deptList = null;
        try {
            deptList = qr.query("select * from dept", new BeanListHandler<>(Dept.class, processor));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deptList;
    }


    public List<Map<String,Object>> findList() {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String,Object>> deptList = null;
        try {
            String sql = "select u.id as userId,u.dept_id as deptId,d.name as deptName,ui.real_name as realName " +
                    "from user u,dept d,user_info ui " +
                    "where u.dept_id = d.id and ui.user_id = u.id";

            deptList = qr.query(sql, new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deptList;
    }
}
